package com.csproj.basic.repository;

import com.csproj.basic.domain.ElectronicLockMessage;
import com.csproj.basic.repository.custom.ElectronicLockMessageRepositoryCustom;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.*;

import java.time.ZonedDateTime;
import java.util.List;


/**
 * Spring Data JPA repository for the ElectronicLockMessage entity.
 */
@SuppressWarnings("unused")
@Repository
public interface ElectronicLockMessageRepository extends JpaRepository<ElectronicLockMessage, String> ,
        ElectronicLockMessageRepositoryCustom, JpaSpecificationExecutor<ElectronicLockMessage> {
    /**
     * 根据ids查询固定读写设备列表信息
     *
     * @param ids
     * @return
     * @author tyshi
     * @date 2018-09-14
     */
    @Query(" from ElectronicLockMessage where deletedFlag = 0 and id in (?1) ")
    List<ElectronicLockMessage> findByIds(String[] ids);


    /**
     * 根据ID删除数据
     *
     * @param id 设备id
     * @author LM
     * @date 2018-09-14
     */
    @Modifying
    @Query("UPDATE ElectronicLockMessage SET enableFlag = ?2 where id = ?1")
    void updateStatus(String id, boolean enable);

    /**
     * 根据ID删除数据
     *
     * @param id 机构id
     * @author LM
     * @date 2018-09-14
     */
    @Query(" from ElectronicLockMessage where deletedFlag = 0 and id = ?1")
    ElectronicLockMessage findById(String id);

    /**
     * 根据机构ids，批量固定读写设备信息
     *
     * @param ids
     */
    @Modifying
    @Query(" update ElectronicLockMessage set deletedFlag = 1 where id in (?1) ")
    void deleteIds(String[] ids);

    /**
     * 获取未分配数量
     *
     * @return
     */
    @Query(" FROM ElectronicLockMessage  where (siteId = '' or siteId is null) and deletedFlag = 0 ")
    List<ElectronicLockMessage> findByUnAlloat();

    @Query(" from ElectronicLockMessage  where deletedFlag = 0 ")
    List<ElectronicLockMessage> findAll();

    ElectronicLockMessage findFirstByElNumberOrderByTimeDesc(String lockNo);

    public Page<ElectronicLockMessage> findAll(Specification specification, Pageable pageable);

    /**
     * 根据调度 获取所有的历史路径信息
     * @param dispatchId
     * @return
     */
    public List<ElectronicLockMessage> findAllByDispatchIdOrderByTimeAsc(String dispatchId);

    public ElectronicLockMessage findFirstByDispatchIdOrderByIdDesc(String dispatchId);

    public List<ElectronicLockMessage> findByDispatchIdAndElNumberAndLatitudeAndLongitudeAndSpeedOrderByTimeDesc(String dispatchId,String elNumber,float latitude,float longitude,float speed);

    @Query(" FROM ElectronicLockMessage where dispatchId=?0 and elNumber=?1 and time >=?2")
    List<ElectronicLockMessage> findForLongTime(String dispatchId,String elNumber,ZonedDateTime dateTime);

    ElectronicLockMessage findFirstByDispatchIdAndElNumberAndIsMovingOrderByTimeDesc(String dispatchId,String elNumber,int isMoving);
}
